package cn.taobaoke.restful.service;

import cn.taobaoke.restful.domain.RobotEntity;
import cn.taobaoke.restful.domain.RobotRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * RobotService类.
 * +-----------------------------------------------------------------------------------------------+
 * |类名称: RobotService                                                                         |
 * |创建时间: 3/25/17                                                                               |
 * |功能简介:                                                                                       |
 * |参考链接:                                                                                       |
 * +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RobotService {


    public List<RobotEntity> getAllRobots(final Long domainId) {
        return robotRepository.findAllByDomainId(domainId);
    }

    public void createOrUpdate(final RobotEntity robot, final Long domainId) {
        val dbRobotOpt = robotRepository.findTopOneByNameAndDomainId(robot.getName(), domainId);
        RobotEntity dbRobot = null;
        if (dbRobotOpt.isPresent()) {

            dbRobot = dbRobotOpt.get();
            if (robot.getName() != null) {
                dbRobot.setName(robot.getName());
            }
            if (robot.getWeChat() != null) {
                dbRobot.setWeChat(robot.getWeChat());
            }
            if (robot.getAlimama() != null) {
                dbRobot.setAlimama(robot.getAlimama());
            }
            if (robot.getAliPassword() != null) {
                dbRobot.setAliPassword(robot.getAliPassword());
            }
            if (robot.getHost() != null) {
                dbRobot.setHost(robot.getHost());
            }
            if (robot.getIp() != null) {
                dbRobot.setIp(robot.getIp());
            }
            if (robot.getMessage() != null) {
                dbRobot.setMessage(robot.getMessage());
            }
            if (robot.getState() != null) {
                dbRobot.setState(robot.getState());
            }
            if (robot.getSendGroups() != null) {
                dbRobot.setSendGroups(robot.getSendGroups());
            }
            dbRobot.setUpdateAt(new Date());
        } else {
            dbRobot = robot;
            dbRobot.setDomainId(domainId);
            dbRobot.setCreateAt(new Date());
            dbRobot.setUpdateAt(new Date());
        }
        robotRepository.save(dbRobot);
    }

    public void deleteById(final Long id, final Long domainId) {
        robotRepository.deleteByIdAndDomainId(id, domainId);
    }

    private final @NonNull RobotRepository robotRepository;
}
